package com.nna88.voz.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nna88.voz.contain.containPage3;
import com.nna88.voz.listview.listViewCustom2.MyGestureDetector;
import com.nna88.voz.main.Page;
import com.nna88.voz.main.R;
import com.nna88.voz.util.UserInfo;

public class listViewCustom3 extends BaseAdapter{
	 public static final int GO_NEXT = 2;
	 public static final int GO_PREVIOUS = 3;
     private ArrayList<containPage3> mcontains;
     private LayoutInflater inflater; 
     private ViewHolder holder;  
     private Drawable d;
     private Activity context;
     private float mTextSize1;
     private float mTextSize2;
     private float mTextSize3;
     private UserInfo mUser;
     private int state;
	 public listViewCustom3(Activity context,ArrayList<containPage3> mcontain)  {  
        //super();      
        this.context = context;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        this.mcontains = mcontain;
        mUser = new UserInfo();
	}  
	 public void setSize(float mTextSize){
	    TextView t1 = (TextView)inflater.inflate(R.layout.list_item3, null).findViewById(R.id.list3_user);
		TextView t2 = (TextView)inflater.inflate(R.layout.list_item3, null).findViewById(R.id.list3_time);
		TextView t3 = (TextView)inflater.inflate(R.layout.list_item3, null).findViewById(R.id.list3_contain);
		this.mTextSize1 = t1.getTextSize()*mTextSize;
		this.mTextSize2 = t2.getTextSize()*mTextSize;
		this.mTextSize3 = t3.getTextSize()*mTextSize;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mcontains.size(); 
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	class ViewHolder  
	{  
		RelativeLayout layout;
        ImageView avatar;  
        TextView user;  
        TextView time;
        TextView index;
        //TextView title;  
        TextView contain;  
   
	}  
	private ColorStateList newColorStateList( int colorDefault, int colorPressed){
		return new ColorStateList(
    			new int[][]{new int [] {android.R.attr.state_pressed},new int [0]},
    			new int []{colorPressed,colorDefault});
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int pos;
		pos = position;
		final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());
        if(convertView==null)  
        {  
            holder = new ViewHolder();  
            convertView = inflater.inflate(R.layout.list_item3, null);  
            holder.layout = (RelativeLayout)convertView.findViewById(R.id.listLayout);  
            holder.avatar = (ImageView) convertView.findViewById(R.id.list3_avatar);  
            holder.user = (TextView) convertView.findViewById(R.id.list3_user);  
            holder.time = (TextView) convertView.findViewById(R.id.list3_time);
           // holder.title = (TextView)convertView.findViewById(R.id.list3Title);
            holder.index = (TextView) convertView.findViewById(R.id.list3_index);
            holder.contain = (TextView) convertView.findViewById(R.id.list3_contain);
            holder.contain.setTextColor(newColorStateList(Color.BLACK, Color.BLUE));
            convertView.setTag(holder);  
        }  
        else  
            holder=(ViewHolder)convertView.getTag();  
        
       // holder.title.setText(mcontains.get(pos).title());
        holder.avatar.setImageBitmap(mcontains.get(position).Avatar());
        holder.user.setText(mcontains.get(position).User());
        holder.contain.setTextSize(mTextSize3);
        holder.contain.setTextColor(newColorStateList(Color.BLACK, Color.BLUE));
        if(mcontains.get(position).Time() != null){
        	holder.time.setText(mcontains.get(position).Time().split(" ")[2] + " " + 
        		mcontains.get(position).Time().split(" ")[3] );
        	holder.index.setText(mcontains.get(position).Time().split(" ")[0]);
        }
        if(mcontains.get(position).isMultiQuote()){
        	holder.contain.setBackgroundResource(R.color.grey);
        }else{
        	holder.contain.setBackgroundResource(R.color.vozThread);
        }
        holder.user.setTextSize(mTextSize1);
        holder.time.setTextSize(mTextSize2);
        holder.index.setTextSize(mTextSize2);
        holder.contain.setText(mcontains.get(position).getText());
        addImage(holder.contain, mcontains.get(position),pos);
        
//        holder.contain.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				if(mItemClickListener != null){
//					mItemClickListener.onItemClick(pos, 1,arg0);
//				}
//			}
//		});
        holder.contain.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				if (gestureDetector.onTouchEvent(event)) {
					if(mItemClickListener != null){
						mItemClickListener.onItemClick(pos, 1,v,state);
					}
                }
                return false;
			}
		});
        holder.contain.setFocusable(true);
        holder.contain.setClickable(true);
//        holder.contain.setFocusable(true);
//        holder.user.setFocusable(true);
//        holder.avatar.setFocusable(true);
//        holder.time.setFocusable(true);
//        holder.index.setFocusable(true);
        return convertView;  
	}
	private OnActionItemClickListener mItemClickListener;
	public void setOnActionItemClickListener(OnActionItemClickListener listener) {
		mItemClickListener = listener;
	}
	public interface OnActionItemClickListener {
		public abstract void onItemClick( int pos, int actionId, View tv, int state);
	}
	class MyGestureDetector extends SimpleOnGestureListener{ 
    	private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;
        // Detect a single-click and call my own handler.
        @Override 
        public boolean onSingleTapUp(MotionEvent e) {
        	//Log.d("nna","single,");
        	state = 0;
            return true;
        }
        @Override 
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        	boolean result = true;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                        	state = GO_PREVIOUS;
                            //Log.d("nna","Right,");
                        } else {
                        	state = GO_NEXT;
                           // Log.d("nna","Left,");
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        } 
    } 
	public void addImage(TextView tview,containPage3 mCon,int position){
		SpannableString ss;
    	ImageSpan span;
    	int i2,start,end;
    	final int pos;
    	pos = position;
    	ss = new SpannableString( mCon.getText()); 
        for( i2=0;i2 < mCon.image.getSize();i2++){
	        d = new BitmapDrawable(context.getResources(),mCon.image.getBitmap(i2));
	        if(mCon.image.getStr(i2).substring(0, 4).equals("http")){
	        	if(d.getIntrinsicWidth() > Page.width*3/4){
	        		d.setBounds(0, 0,Page.width*3/4,  
	        				d.getIntrinsicHeight()*Page.width*3/4/d.getIntrinsicWidth());
	        	}else d.setBounds(0, 0, d.getIntrinsicWidth(),  d.getIntrinsicHeight());
	        }else{
	        	d.setBounds(0, 0, (int)(tview.getTextSize()*1.5), (int)(tview.getTextSize()*1.5));
	        }
	        span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM); 
	        start = mCon.image.getStart(i2);
	        end = mCon.image.getEnd(i2);
	        
	        ss.setSpan(span,start,end, SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
    	}
    	for(i2=0;i2< mCon.quote.getSize();i2++){
    		start = mCon.quote.getStart(i2);
 	        end = mCon.quote.getEnd(i2);
 	        ss.setSpan(new  QuoteSpan(R.color.ics_blue_dark), start, end,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
 	        ss.setSpan( new StyleSpan(Typeface.ITALIC),start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE); 
 	        ss.setSpan(new ForegroundColorSpan(Color.GRAY), start, end,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    	}
    	for(i2=0;i2< mCon.web.getSize();i2++){
    		start = mCon.web.getStart(i2);
 	        end = mCon.web.getEnd(i2);
 	        ss.setSpan(new URLSpan(mCon.web.getStr(i2)), start, end,
	                   Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    	}
    	for(i2=0;i2< mCon.font.getSize();i2++){
           	start = mCon.font.getStart(i2);
  	       	end = mCon.font.getEnd(i2);
 	       	if(mCon.font.color(i2).contains("ed"))//red
 	       		ss.setSpan( new ForegroundColorSpan(Color.RED), start, end,	Spanned.SPAN_INCLUSIVE_INCLUSIVE);
 	       	else if(mCon.font.color(i2).contains("lue")){//blue
 	       		ss.setSpan( new ForegroundColorSpan(Color.BLUE), start, end,	Spanned.SPAN_INCLUSIVE_INCLUSIVE);
 	       	}else if(mCon.font.color(i2).contains("reed")){//greed
 	       		ss.setSpan( new ForegroundColorSpan(Color.GREEN), start, end,	Spanned.SPAN_INCLUSIVE_INCLUSIVE);
 	       	}
 	       	ss.setSpan( new RelativeSizeSpan((float) (mCon.font.size(i2)/3.)),
 	       			start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    	}
    	for(i2=0;i2< mCon.type.getSize();i2++){
           	start = mCon.type.getStart(i2);
 	       	end = mCon.type.getEnd(i2);
  	    	ss.setSpan( new StyleSpan(mCon.type.type()),
 	       			start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);  
    	}
    	for(i2=0;i2< mCon.typeU.getSize();i2++){
           	start = mCon.typeU.getStart(i2);
 	       	end = mCon.typeU.getEnd(i2);
  	    	ss.setSpan( new UnderlineSpan(),
 	       			start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    	}
     	tview.setText(ss);
    	tview.setClickable(false);
    	tview.setFocusable(false);
    	tview.setFocusableInTouchMode(false);
    	tview.setMovementMethod(LinkMovementMethod.getInstance());
    }

	
}
